package com.spzx.order.receivers;

import com.rabbitmq.client.Channel;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.order.service.OrderInfoService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrderReceiver {

    @Autowired
    OrderInfoService orderInfoService;


    @RabbitListener(queues = "queue.delay.1")
    public void cancelOrder(Channel channel, Message message, String orderNo){
         //MQ的防止重复消费请求的幂等性判断
        System.out.println("幂等性校验");
        System.out.println("延迟关单，订单号："+orderNo);
        int i = orderInfoService.getOrderStatus(orderNo);
        if(i==0){
            //修改订单状态
            orderInfoService.cancelOrder(orderNo);
            System.out.println("延迟关单");
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(durable = "true",value = MqConst.EXCHANGE_PAYMENT_PAY),
            key = {MqConst.ROUTING_PAYMENT_PAY},
            value = @Queue(durable = "true",value = MqConst.QUEUE_PAYMENT_PAY)
    ))
    public void payOrder(Channel channel, Message message,String orderNo){
        System.out.println("监听支付结果过");
        System.out.println("更新订单信息：已支付");
        // MQ的防止重复消费请求的幂等性判断
        System.out.println("幂等性校验");
        int i = orderInfoService.getOrderStatus(orderNo);
        if(i==0){
            //修改订单状态
            orderInfoService.payOrder(orderNo);
            System.out.println("修改订单状态：已支付");
        }

    }

}